namespace SmartSoft.Controls
{
    using SmartSoft;
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;

    public class TextOption : TextControl
    {
        protected Hashtable FirstTable = new Hashtable();
        protected int ii_KeyOrder;
        private string is_InputMask = "";
        protected string is_LastInputMask = "";
        protected Hashtable KeyTable = new Hashtable();

        public TextOption()
        {
            base.GotFocus += new EventHandler(this.TextOption_GotFocus);
        }

        public bool CheckKeys(string is_Keys, char is_Char)
        {
            bool flag = true;
            if (is_Keys.Length == 1)
            {
                char[] chArray = is_Keys.ToCharArray();
                for (int i = 0; i < chArray.Length; i++)
                {
                    if (!char.IsLetter(chArray[i]))
                    {
                        return false;
                    }
                }
                return flag;
            }
            if (!char.IsNumber(is_Char))
            {
                flag = false;
            }
            return flag;
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if ((keyData != Keys.Delete) && (keyData != Keys.Back))
            {
                if (keyData == Keys.Space)
                {
                    this.Text = this.SearchString();
                    return true;
                }
                if (this.FirstTable.ContainsValue(keyData.ToString()))
                {
                    this.Text = this.SearchString(keyData.ToString());
                    return true;
                }
                if (!this.CheckKeys(keyData.ToString(), (char) ((int) msg.WParam)))
                {
                    this.OnValidated(EventArgs.Empty);
                    return base.ProcessCmdKey(ref msg, keyData);
                }
            }
            return true;
        }

        public string SearchString()
        {
            if (this.KeyOrder == (this.KeyTable.Count - 1))
            {
                this.KeyOrder = 0;
            }
            else
            {
                this.KeyOrder++;
            }
            return (string) this.KeyTable[this.KeyOrder];
        }

        public string SearchString(string ps_String)
        {
            int num = -1;
            if (this.KeyOrder == (this.FirstTable.Count - 1))
            {
                for (int i = 0; i < (this.FirstTable.Count - 1); i++)
                {
                    if (((string) this.FirstTable[i]) == ps_String)
                    {
                        num = i;
                        break;
                    }
                }
            }
            else
            {
                Hashtable hashtable = new Hashtable();
                for (int j = this.KeyOrder + 1; j < this.FirstTable.Count; j++)
                {
                    hashtable.Add(j, this.FirstTable[j]);
                }
                for (int k = this.KeyOrder + 1; k < this.FirstTable.Count; k++)
                {
                    if (((string) hashtable[k]) == ps_String)
                    {
                        num = k;
                        break;
                    }
                }
                if (num == -1)
                {
                    for (int m = 0; m < this.KeyOrder; m++)
                    {
                        if (((string) this.FirstTable[m]) == ps_String)
                        {
                            num = m;
                            break;
                        }
                    }
                }
            }
            if (num != -1)
            {
                this.KeyOrder = num;
            }
            return (string) this.KeyTable[this.KeyOrder];
        }

        private void TextOption_GotFocus(object sender, EventArgs e)
        {
            base.SelectionStart = 0;
        }

        [Category("Text option settings")]
        public string InputMask
        {
            get
            {
                return this.is_InputMask;
            }
            set
            {
                if (value != this.is_InputMask)
                {
                    this.is_InputMask = value;
                    if (this.FirstTable.Count > 0)
                    {
                        this.FirstTable.Clear();
                        this.KeyTable.Clear();
                    }
                    int key = 0;
                    string str = this.is_InputMask.Trim();
                    string str2 = "";
                    while (str.Contains(","))
                    {
                        string str3 = str.Substring(0, str.IndexOf(",")).Trim();
                        str = str.Substring(str.IndexOf(",") + 1, (str.Length - str.IndexOf(",")) - 1).Trim();
                        if (key == 0)
                        {
                            str2 = str3;
                        }
                        this.FirstTable.Add(key, str3.Substring(0, 1));
                        this.KeyTable.Add(key, str3);
                        key++;
                    }
                    this.FirstTable.Add(key, str.Substring(0, 1));
                    this.KeyTable.Add(key, str);
                    this.Text = str2;
                    this.KeyOrder = 0;
                }
            }
        }

        protected int KeyOrder
        {
            get
            {
                return this.ii_KeyOrder;
            }
            set
            {
                this.ii_KeyOrder = value;
            }
        }

        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                if (!this.InputMask.Equals(string.Empty) && !this.KeyTable.ContainsValue(value))
                {
                    MessageBox.Show("Value must be in Inputmask!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                else
                {
                    for (int i = 0; i < this.KeyTable.Count; i++)
                    {
                        if (((string) this.KeyTable[i]) == value)
                        {
                            this.KeyOrder = i;
                            break;
                        }
                    }
                    base.Text = value;
                }
            }
        }
    }
}

